#ifndef PLAYERLISTH
#define PLAYERLISTH

#include <map>
#include <vector>
#include <string>
#include <gmMachine.h>
#include <gmUserObject.h>
#include "ByteStream.h"
#include "Player.h"

class PlayerList
{
private:
    std::vector<GMPlayer*>      m_playerList;
    std::map<int, GMPlayer*>    m_playerIDs;

public:
    //Add player to this list
    void AddPlayer(int playerID, GMPlayer* player);

    //Remove player from this list
    void RemovePlayer(int playerID);

    //Send packet to all players in this list (except to sender and only of type "clientType)
    void WritePacket(const ByteStream& packet, const Player* sender = NULL, int clientType = 0);

    GMPlayer* FindByName(const std::string& name);
    GMPlayer* FindByID(int id);
    GMPlayer* GetAt(size_t index);

    size_t PlayerCount() const {
        return m_playerList.size();
    }


};

class GMPlayerList:
    public PlayerList
{
private:
    gmUserObject*   m_userObject;
    static gmType   m_userType;

public:
    GMPlayerList(gmMachine* machine) {
        m_userObject = machine->AllocUserObject(this, m_userType);
    }

    ~GMPlayerList() {
        m_userObject->m_user = NULL;
    }

    gmUserObject* GetUserObject() {
        return m_userObject;
    }

    static gmType GetType() {
        return m_userType;
    }

    static int GM_CDECL GMFindByName(gmThread *a_thread);
    static int GM_CDECL GMFindByID(gmThread *a_thread);
    static void GM_CDECL GMOPIndexGet(gmThread * a_thread, gmVariable * a_operands);
    static void Register(gmMachine* machine);
};

#endif // PLAYERLISTH

